package com.etime.shycourse.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.dto.CouponMyDto;
import com.etime.shycourse.dto.CouponRangeDto;
import com.etime.shycourse.dto.CouponTypesDto;
import com.etime.shycourse.pojo.Coupon;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 *
 * @author wb
 * @since 2022-03-15
 */
@Repository
public interface CouponMapper extends BaseMapper<Coupon> {

    /**
     * 根据活动id查询活动所包含的优惠券列表
     * 优惠券必须是已发布状态。
     *
     * @param activityid 活动id
     * @return
     */
//    @Select("SELECT a.couponid,coupontitle,couponimg, " +
//            "coupondesc,coupontype,couponcoursecount," +
//            "couponmoney,coupondiscount,couponfull,couponstate,couponallowdre " +
//            "FROM coupon a inner join couponactivityref b on a.couponid = b.couponid " +
//            "where b.cactivityid=#{activityid} and a.couponstate=2")
    @Select("SELECT a.* " +
            "FROM coupon a inner join couponactivityref b on a.couponid = b.couponid " +
            "where b.cactivityid=#{activityid} and a.couponstate=2")
    List<Coupon> selectCouponListByActivityId(@Param("activityid") Integer activityid);

    /**
     * 查询所有的优惠劵  分页
     * 以创建时间 降序  + 优惠劵状态 升序 1 创建未发布 2 创建发布 3 关闭
     * ouponcreatetime desc,couponstate asc
     * coupontype可根据 优惠券类型进行查询
     * couponstate 可根据 优惠券状态进行查询
     * <p></p>
     *
     * @param coupon 条件
     * @param page,  分页
     * @return Page<CouponTypesDto>
     * @author wb
     */
    Page<CouponTypesDto> selectCouponTypesDtoPage(Coupon coupon, Page<CouponTypesDto> page);

    /**
     * 根据用户id查询用户的可用优惠券列表
     *
     * @param userid
     * @return
     */
    @Select("SELECT " +
            "a.couponid,a.coupontitle,a.couponimg,a.coupontype, " +
            "a.couponcoursecount,a.couponmoney,a.coupondiscount, " +
            "a.couponcourseimpose,a.couponfull,a.couponstate,b.couponreciveid, " +
            "b.couponno,b.couponrecivestate,b.couponrecivebegin,b.couponreciveend, " +
            "b.couponreciveuserid,a.couponeffectivetype " +
            "FROM coupon a " +
            "inner join couponrecive b on a.couponid=b.couponid " +
            "where b.couponreciveuserid=#{userid} and b.couponrecivestate=1 " +
            "and (b.couponreciveend>=curdate() or b.couponreciveend is null) ")
    List<CouponMyDto> selectMyCouponList(@Param("userid") Integer userid);

    /**
     * 根据优惠券id查询优惠券的使用范围
     *
     * @param couponid 优惠券id
     * @return
     */
    @Select("select a.couponid,a.courseorpackage,a.rangecourseid,a.rangpackageid,b.coursetitle, " +
            "c.packagename " +
            "from couponrange a " +
            "left outer join course b on a.rangecourseid = b.courseid " +
            "left outer join coursepackage c on a.rangpackageid=c.packageid " +
            "where couponid=#{couponid}")
    List<CouponRangeDto> selectVoucherRang(@Param("couponid") Integer couponid);

    /**
     * 根据领取id查询优惠券的详细信息
     *
     * @param reciveid
     * @return
     */
    @Select("select a.*,b.couponreciveid,b.couponno,b.couponreciveuserid," +
            "b.couponreciveusername, " +
            "b.couponrecivestate,b.couponrecivebegin,b.couponreciveend from coupon a " +
            "inner join couponrecive b on a.couponid=b.couponid " +
            "where b.couponreciveid=#{reciveid}")
    CouponMyDto selectCouponByReciveId(@Param("reciveid") Integer reciveid);

    /**
     * 查询所有的优惠劵 用活动管理的优惠劵展示
     * 条件：
     * 1、Couponstate = 2 创建发布状态
     * 2、优惠券的发放数量<总数量 或者总数量=0
     * 3、优惠劵类型 1、3、2（优惠券结束使用时间>活动结束时间）
     */
    List<Coupon> selectByActivityList(Date activeend);

}
